package com.ruoyi.net.service.impl;

import com.ruoyi.common.constant.ElementConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.domain.*;
import com.ruoyi.net.mapper.*;
import com.ruoyi.net.service.INetElementLinkService;
import com.ruoyi.net.utils.autoIncrement.GlobalIncrementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static com.ruoyi.common.constant.ElementConstants.*;

/**
 * 网元设备连线Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-27
 */
@Service
public class NetElementLinkServiceImpl implements INetElementLinkService 
{
    @Autowired
    private NetElementLinkMapper netElementLinkMapper;

    @Autowired
    private NetElementPortMapper netElementPortMapper;

    @Autowired
    private ElementPortModeMapper elementPortModeMapper;

    @Autowired
    private NetElementMapper netElementMapper;

    @Autowired
    private BusiElementStatusMapper busiElementStatusMapper;

    @Autowired
    private BusiNeighbLabelMapper busiNeighbLabelMapper;

    @Autowired
    private ElementPortEthMapper elementPortEthMapper;

    @Autowired
    GlobalIncrementService globalIncrementService;

    /**
     * 查询网元设备连线
     * 
     * @param id 网元设备连线主键
     * @return 网元设备连线
     */
    @Override
    public NetElementLink selectNetElementLinkById(String id)
    {
        return netElementLinkMapper.selectNetElementLinkById(id);
    }

    /**
     * 查询网元设备连线列表
     * 
     * @param netElementLink 网元设备连线
     * @return 网元设备连线
     */
    @Override
    public List<NetElementLink> selectNetElementLinkList(NetElementLink netElementLink)
    {
        return netElementLinkMapper.selectNetElementLinkList(netElementLink);
    }

    /**
     * 新增网元设备连线
     * 
     * @param netElementLink 网元设备连线
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNetElementLink(NetElementLink netElementLink)
    {
        // 更新端口绑定状态，绑定
        ElementPortMode startPort = new ElementPortMode();
        startPort.setElementId(netElementLink.getStartElementId());
        startPort.setId(netElementLink.getStartPortId());
        startPort.setBindStatus(STATUS_BIND_ON);
        startPort.setBindTime(DateUtils.getNowDate());
        startPort.setUpdateBy(SecurityUtils.getUsername());
        startPort.setUpdateTime(DateUtils.getNowDate());
//        startPort.setStatus(STATUS_OFF);
        elementPortModeMapper.updateElementPortModeBindStatus(startPort);

        ElementPortMode endPort = new ElementPortMode();
        endPort.setElementId(netElementLink.getEndElementId());
        endPort.setId(netElementLink.getEndPortId());
        endPort.setBindStatus(STATUS_BIND_ON);
        endPort.setBindTime(DateUtils.getNowDate());
        endPort.setCreateBy(SecurityUtils.getUsername());
        endPort.setUpdateTime(DateUtils.getNowDate());
//        endPort.setStatus(STATUS_OFF);
        elementPortModeMapper.updateElementPortModeBindStatus(endPort);
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(netElementLink.getId())) {
            netElementLink.setId(uuid);
        }
        ElementPortMode startElementPortMode = elementPortModeMapper.selectElementPortModeById(netElementLink.getStartPortId());
        ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(netElementLink.getEndPortId());

        //2.与element_port_eth以太表接口名称对比  找出源接口名称相同的数据拿到 L3 使能 ip等判断数据
        ElementPortEth elementPortEth = new ElementPortEth();
        elementPortEth.setElementId(netElementLink.getStartElementId());
        List<ElementPortEth> startElementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);
        ElementPortEth finalStartPortEth = new ElementPortEth();
        for (ElementPortEth temp:startElementPortEths){
            //关联关系为以太的接口名（portName）和接口模式配置表的接口名（portName）
            if (temp.getPortName().equals(startElementPortMode.getPortName())){
                finalStartPortEth.setTerminalMode(temp.getTerminalMode());
                finalStartPortEth.setMplsEnable(temp.getMplsEnable());
                finalStartPortEth.setIp(temp.getIp());
            }
        }
        //与element_port_eth以太表接口名称对比  找出宿接口名称相同的数据拿到 L3 使能 ip等判断数据
        elementPortEth.setElementId(netElementLink.getEndElementId());
        List<ElementPortEth> endElementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);
        ElementPortEth finalEndPortEth = new ElementPortEth();
        for (ElementPortEth temp:endElementPortEths){
            if (temp.getPortName().equals(endElementPortMode.getPortName())){
                finalEndPortEth.setTerminalMode(temp.getTerminalMode());
                finalEndPortEth.setMplsEnable(temp.getMplsEnable());
                finalEndPortEth.setIp(temp.getIp());
            }
        }
        //新增邻接标签对象 2.9修改逻辑：源宿都为以太接口且端口模式=L3 mpls=使能 有主用IPV4地址才新增邻接标签
        if ("enable".equals(startElementPortMode.getPortMode()) && "enable".equals(endElementPortMode.getPortMode())&&
                "layer-3-route".equals(finalStartPortEth.getTerminalMode()) && "layer-3-route".equals(finalEndPortEth.getTerminalMode())&&
                StringUtils.isNotEmpty(finalStartPortEth.getIp()) && StringUtils.isNotEmpty(finalEndPortEth.getIp())&&
                "enable".equals(finalStartPortEth.getMplsEnable()) && "enable".equals(finalEndPortEth.getMplsEnable())) {
            //新增连接标签逻辑
            BusiNeighbLabel busiNeighbLabel = new BusiNeighbLabel();
            busiNeighbLabel.setId(IdUtils.simpleUUID());
            busiNeighbLabel.setStartElement(netElementLink.getStartElementId());
            busiNeighbLabel.setEndElement(netElementLink.getEndElementId());
            busiNeighbLabel.setStartPortId(netElementLink.getStartPortId());
            busiNeighbLabel.setEndPortId(netElementLink.getEndPortId());
            busiNeighbLabel.setLabelType("全局");
            busiNeighbLabel.setStartNeighbLabel(globalIncrementService.getNextInLabelIncrementNumber());
            busiNeighbLabel.setEndNeighbLabel(globalIncrementService.getNextInLabelIncrementNumber());
            busiNeighbLabel.setCreateBy(SecurityUtils.getUsername());
            busiNeighbLabel.setCreateTime(DateUtils.getNowDate());
            busiNeighbLabel.setUpdateBy(SecurityUtils.getUsername());
            busiNeighbLabel.setUpdateTime(DateUtils.getNowDate());
            //设置源对端ip
            busiNeighbLabel.setStartOppositeIp(finalEndPortEth.getIp());
            //设置宿对端ip
            busiNeighbLabel.setEndOppositeIp(finalStartPortEth.getIp());
            busiNeighbLabelMapper.insertBusiNeighbLabel(busiNeighbLabel);
            //邻接标签两端插入设备状态表，以便控制部分激活状态
            insertBusiElementStatusByNeighb(busiNeighbLabel);
        }
        netElementLink.setLinkType("1");
        netElementLink.setCreateBy(SecurityUtils.getUsername());
        netElementLink.setCreateTime(DateUtils.getNowDate());
        return netElementLinkMapper.insertNetElementLink(netElementLink);
    }

    /**
     * 通过
     * @param busiNeighbLabel
     */
    private void insertBusiElementStatusByNeighb(BusiNeighbLabel busiNeighbLabel) {
        //源设备状态表
        BusiElementStatus busiElementStatus = new BusiElementStatus();
        busiElementStatus.setId(IdUtils.simpleUUID());
        busiElementStatus.setStatus("0");
        busiElementStatus.setBusiId(busiNeighbLabel.getId());
        busiElementStatus.setCreateTime(DateUtils.getNowDate());
        busiElementStatus.setUpdateTime(DateUtils.getNowDate());
        busiElementStatus.setCreateBy(SecurityUtils.getUsername());
        busiElementStatus.setUpdateBy(SecurityUtils.getUsername());
        busiElementStatus.setBusiType(BUSI_NEIGHB);
        busiElementStatus.setElementId(busiNeighbLabel.getStartElement());
        //源插入
        busiElementStatusMapper.insertBusiElementStatus(busiElementStatus);

        busiElementStatus.setId(IdUtils.simpleUUID());
        busiElementStatus.setElementId(busiNeighbLabel.getEndElement());
        busiElementStatus.setCreateTime(DateUtils.getNowDate());
        busiElementStatus.setUpdateTime(DateUtils.getNowDate());
        busiElementStatus.setCreateBy(SecurityUtils.getUsername());
        busiElementStatus.setUpdateBy(SecurityUtils.getUsername());
        //宿插入
        busiElementStatusMapper.insertBusiElementStatus(busiElementStatus);

    }

    /**
     * 修改网元设备连线
     * 
     * @param netElementLink 网元设备连线
     * @return 结果
     */
    @Override
    public int updateNetElementLink(NetElementLink netElementLink)
    {
        netElementLink.setUpdateBy(SecurityUtils.getUsername());
        netElementLink.setUpdateTime(DateUtils.getNowDate());
        return netElementLinkMapper.updateNetElementLink(netElementLink);
    }

    /**
     * 批量删除网元设备连线
     * 
     * @param ids 需要删除的网元设备连线主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNetElementLinkByIds(String[] ids)
    {
        for (String id : ids) {
            NetElementLink netElementLink = netElementLinkMapper.selectNetElementLinkById(id);
            if (netElementLink != null) {
                // 更新端口绑定状态，解绑
                ElementPortMode startPort = new ElementPortMode();
                startPort.setElementId(netElementLink.getStartElementId());
                startPort.setId(netElementLink.getStartPortId());
                startPort.setBindStatus(STATUS_BIND_OFF);
                startPort.setUpdateBy(SecurityUtils.getUsername());
                startPort.setUpdateTime(DateUtils.getNowDate());
//                startPort.setStatus(STATUS_ON);
                elementPortModeMapper.updateElementPortModeBindStatus(startPort);

                ElementPortMode endElementPort = new ElementPortMode();
                endElementPort.setId(netElementLink.getEndPortId());
                endElementPort.setElementId(netElementLink.getEndElementId());
                endElementPort.setBindStatus(STATUS_BIND_OFF);
                endElementPort.setUpdateBy(SecurityUtils.getUsername());
                endElementPort.setUpdateTime(DateUtils.getNowDate());
//                endElementPort.setStatus(STATUS_ON);
                elementPortModeMapper.updateElementPortModeBindStatus(endElementPort);
                //删除邻接标签对象
                BusiNeighbLabel busiNeighbLabel = new BusiNeighbLabel();
                busiNeighbLabel.setStartElement(netElementLink.getStartElementId());
                busiNeighbLabel.setEndElement(netElementLink.getEndElementId());
                busiNeighbLabel.setStartPortId(netElementLink.getStartPortId());
                busiNeighbLabel.setEndPortId(netElementLink.getEndPortId());
                List<BusiNeighbLabelListVo> busiNeighbLabels = busiNeighbLabelMapper.selectBusiNeighbLabelList(busiNeighbLabel);
                if (!CollectionUtils.isEmpty(busiNeighbLabels)){
                    busiNeighbLabelMapper.deleteBusiNeighbLabelById(busiNeighbLabels.get(0).getId());
                }
            }

        }
        return netElementLinkMapper.deleteNetElementLinkByIds(ids);
    }

    /**
     * 删除网元设备连线信息
     * 
     * @param id 网元设备连线主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNetElementLinkById(String id)
    {
        NetElementLink netElementLink = netElementLinkMapper.selectNetElementLinkById(id);
        if (netElementLink != null) {
            // 更新端口绑定状态，解绑
            ElementPortMode netElementPort = new ElementPortMode();
            netElementPort.setElementId(netElementLink.getStartElementId());
            netElementPort.setBindStatus("0");
            netElementPort.setUpdateBy(SecurityUtils.getUsername());
            netElementPort.setUpdateTime(DateUtils.getNowDate());
//            netElementPort.setStatus(STATUS_OFF);
            elementPortModeMapper.updateElementPortModeBindStatus(netElementPort);
        }
        return netElementLinkMapper.deleteNetElementLinkById(id);
    }
}
